class OrderExaminationsForTechnikController < ApplicationController
  before_filter :update_table_config

  active_scaffold :order_examination  do |config|
    config.label = " Zlecenia"
    config.columns = [
            :id,
            #:examination_type_name,
            :priorytet,
            :status,
            :user,
            :executing_user,
            :checking_user
    ]

    columns[:id].label = 'Nr. Zlecenia'

    #columns[:examination_type_name].label = 'Nazwa Badania'

    columns[:created_at].label = "Utworzono"
    columns[:updated_at].label = "Zmodyfikowano"
    columns[:status].label = "Status badania"
    columns[:user].label = "Zlecajacy"
    columns[:user].clear_link
    columns[:executing_user].label = "Wykonujacy"
    columns[:executing_user].clear_link
    columns[:checking_user].label = "Sprawdzajacy"
    columns[:checking_user].clear_link

    #columns[:status].set_link 'Toggle', :action => 'toggle_active', :position => false
    #config.action_links.add 'toggle_active', :label => 'Dodaj wynik', :position => false, :page => true
    config.action_links.add 'toggle_active', :label => 'Opcje', :type => :record, :inline => true , :position => false 
    config.actions.exclude :update, :delete, :create
    list.per_page = 15
  end

  
   def toggle_active
    @remsite = OrderExamination.find(params[:id])

    if  @remsite.has_resoult != true and  @remsite.is_checked != true
      active_scaffold_config.action_links.add "add_resoult", :label => 'Dodaj wynik', :type => :record, :page => true, :position => false
    else if @remsite.is_checked != true and  @remsite.has_resoult == true
      active_scaffold_config.action_links.add 'check', :label => 'Sprawdź poprawność', :type => :record, :page => true, :security_method => :respw_authorized?

    end
    end
    @remsite.save

     #redraw just the line in question
     #render :partial => 'list_record', :locals => {:record => @remsite}
     #render :action => 'list', :locals => {:record => @remsite}
     #render :action => 'toggle_active.rjs', :locals => {:record => @remsite}
     end

  def update_table_config

  end


  #def active_scaffold_refresh_row(record = nil)
  #  record = @record unless record
  #  row_id = element_row_id(:action => :list, :id => record.id)
  #  render :update do |page|
  #    page.replace row_id, :partial => 'list_record', :locals => { :record => record }
  #    page << "ActiveScaffold.stripe('#{active_scaffold_tbody_id}');"
  #    page.replace_html active_scaffold_messages_id, :partial =>'messages'
  #    page.visual_effect :highlight, row_id
  #  end
  #end

  def respw_authorized?
    (current_user.roles.detect { |role| role.id == 4 })
  end

  def after_update_save(record)
    flash[:info] = 'Zlecenie zostało zmienione'
  end

  def conditions_for_collection
    ['is_checked = false']
  end


  def add_resoult
    order_examination = OrderExamination.find(params[:id])

    @exam_resoult = TempExaminationResoult.find_by_order_examination_id order_examination.id
    if @exam_resoult == nil
#      @exam_resoult = TempExaminationResoult.create(
#        :order_examination_id => order_examination.id,
#        :user_id => current_user.id
#      )

      @exam_resoult = TempExaminationResoult.new(
              :order_examination_id => order_examination.id,
              :user_id => current_user.id
      )

      examination_type = order_examination.examination_type
      for p_norm in examination_type.param_norms
        @exam_resoult.param_resoults.build(:param_norm_id => p_norm.param_norm_id)
      end
    end

  else
    OrderExamination.update(params[:id], :has_resoult => true, :executing_user_id => current_user.id)
  end

  def check
    order_examination = OrderExamination.find(params[:id])
    @new_examination_resoult = TempExaminationResoult.find_by_order_examination_id order_examination.id
  end
end
